package ar.edu.itba.it.pdc.httpParser.models;

import java.nio.ByteBuffer;
import java.util.Queue;

import ar.edu.itba.it.pdc.httpParser.interfaces.Model;
import ar.edu.itba.it.pdc.nio.MemoryAllocationLimitHited;

public abstract class HttpMessage implements Model {

	HttpHeaders headers;
	HttpBody body;
	
	public abstract void setInitialLine(Model initialLine);
	
	protected abstract Model getInitialLine();

	public void setHeaders(HttpHeaders headers) {
		this.headers = headers;
	}

	public void setBody(HttpBody body) {
		this.body = body;
	}

	public String getHeaderValue(String string) {
		return headers.getHeader(string);
	}
	
	public boolean removeHeader(String headerName){
		return headers.removeHeader(headerName);
	}
	
	public void addHeader(String headerName, String headerValue){
		headers.put(headerName, headerValue);
	}
	
	public int getBodySize() {
		int size = 0;
		for (Token token : this.body.tokens) {
			size += token.length();
		}
		return size;
	}
	
	public HttpBody getBody() {
		return body;
	}
	
	@Override
	public void appendByteBuffers(Queue<ByteBuffer> queue) throws MemoryAllocationLimitHited {
		getInitialLine().appendByteBuffers(queue);
		headers.appendByteBuffers(queue);
		body.appendByteBuffers(queue);
	}
	
	public HttpHeaders getHeaders() {
		return headers;
	}
	
	public abstract boolean isConnectionClose();
	public abstract boolean closeConnection();
}
